package com.cxl.demo.mapper;

import com.cxl.demo.vo.Comment;
import lombok.Data;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface CommentMapper {

    @Insert("insert into comment(parentCommentId,assigmentId,topId,userId,content,createTime) values(" +
            "#{parentCommentId},#{assigmentId},#{topId},#{userId},#{content},#{createTime})")
    @Options(useGeneratedKeys = true,keyProperty = "commentId",keyColumn = "commentId")
    Integer add(Comment comment);
    @Delete("delete from comment where commentId=#{commentId}")
    Integer DelCommentBuyId(Integer CommentId);
    @Select("select * from comment where assigmentId=#{parentCommentId} and parentCommentId is null")
    @Results(value={
            @Result(property = "commentId",column = "commentId"),
            @Result(property = "parentCommentId",column = "parentCommentId"),
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "topId",column = "topId"),
            @Result(property = "content",column = "content"),
            @Result(property = "createTime",column = "createTime"),
            @Result(property = "user",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "children",column = "commentId",
                    one = @One(select = "com.cxl.demo.mapper.CommentMapper.getCommentsByTopId"))
    })
    List<Comment> getCommentsBuAssigmentId(Integer assigmentId);




    @Select("select * from comment where topId=#{topId}")
    @Results(value={
            @Result(property = "commentId",column = "commentId"),
            @Result(property = "parentCommentId",column = "parentCommentId"),
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "content",column = "content"),
            @Result(property = "createTime",column = "createTime"),
            @Result(property = "user",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId")),
            @Result(property = "parent",column = "parentCommentId",
                    one = @One(select = "com.cxl.demo.mapper.CommentMapper.getCommentsByCommentId"))

    })
    List<Comment> getCommentsByTopId(Integer topId);

    @Select("select * from comment where commentId=#{commentId}")
    @Results(value={
            @Result(property = "commentId",column = "commentId"),
            @Result(property = "parentCommentId",column = "parentCommentId"),
            @Result(property = "assigmentId",column = "assigmentId"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "content",column = "content"),
            @Result(property = "createTime",column = "createTime"),
            @Result(property = "user",column = "userId",
                    one = @One(select = "com.cxl.demo.mapper.UserMapper.findUserInfoByUserId"))
    })
    Comment getCommentsByCommentId(Integer commentId);






}
